package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add ( checkItem );
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage ();
        System.out.println (currentPage);
        Integer pageSize = queryPageBean.getPageSize ();
        System.out.println (pageSize);
        String queryString = queryPageBean.getQueryString ();
        PageHelper.startPage ( currentPage, pageSize );
        Page<CheckItem> page1 = checkItemDao.findPage ( queryString );
        List<CheckItem> pageResult = page1.getResult ();
        long total = page1.getTotal ();
        return new PageResult ( total,pageResult );
    }

    @Override
    public void delete(Integer id) {
        Long count = checkItemDao.findById ( id );
        if(count>0){
          throw  new RuntimeException ("当前检查项被引用，不能删除");
        }else{
            checkItemDao.delete ( id );
        }

    }

    @Override
    public CheckItem findItemById(Integer id) {
        return checkItemDao.findItemById ( id );
    }

    @Override
    public void update(CheckItem checkItem) {
        checkItemDao.update ( checkItem );
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll ();
    }
}
